/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.mes.service.impl;

import me.zhengjie.modules.mes.domain.MesMenu;
import me.zhengjie.modules.system.domain.Menu;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.mes.service.MesMenuService;
import me.zhengjie.modules.mes.domain.vo.MesMenuQueryCriteria;
import me.zhengjie.modules.mes.mapper.MesMenuMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author mfc
* @date 2024-12-31
**/
@Service
@RequiredArgsConstructor
public class MesMenuServiceImpl extends ServiceImpl<MesMenuMapper, MesMenu> implements MesMenuService {

    private final MesMenuMapper mesMenuMapper;

    @Override
    public PageResult<MesMenu> queryAll(MesMenuQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(mesMenuMapper.findAll(criteria, page));
    }

    @Override
    public List<MesMenu> queryAll(MesMenuQueryCriteria criteria){
        return mesMenuMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(MesMenu resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(MesMenu resources) {
        MesMenu mesMenu = getById(resources.getMenuId());
        mesMenu.copy(resources);
        saveOrUpdate(mesMenu);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<MesMenu> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (MesMenu mesMenu : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("上级菜单ID", mesMenu.getPid());
            map.put("菜单标题", mesMenu.getTitle());
            map.put("排序", mesMenu.getMenuSort());
            map.put("链接地址", mesMenu.getPath());
            map.put("隐藏", mesMenu.getHidden());
            map.put("权限", mesMenu.getPermission());
            map.put("创建者", mesMenu.getCreateBy());
            map.put("更新者", mesMenu.getUpdateBy());
            map.put("创建日期", mesMenu.getCreateTime());
            map.put("更新时间", mesMenu.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<MesMenu> getChildMenus(Long pid) {
        List<MesMenu> menus;
        if(pid != null && !pid.equals(-1L)){
            menus = mesMenuMapper.findByPidOrderByMenuSort(pid);
        } else {
            menus = mesMenuMapper.findByPidIsNullOrderByMenuSort();
        }
        return menus;
    }
}